package com.baozun.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.baozun.model.TContact;
import com.baozun.model.TUser;

/**
 * 
 * @Title:
 * @Company:宝尊电子商务有限公司
 * @Author:万世极缘
 * @Since:2015-1-8
 * @Copyright:Copyright (c) 2015
 * @ModifyDate:2015-1-8
 * @Version:1.1.0
 */
public class ContactUtil {

    /**
     * 获取当前用户能够使用的用户
     * 
     * @param contacts
     * @param user
     * @return
     */
    public static List<TContact> getUseableContact(List<TContact> contacts, TUser user) {
        List<TContact> contactList = new ArrayList<TContact>();
        for (TContact contact : contacts) {
            if (user.getStUserId().equals(contact.getStUserId()) || CommonConst.BOOLEAN_VALUE_TRUE_STR.equals(contact.getStIsOpen())) {
                contactList.add(contact);
            }
        }
        return contactList;
    }

    /**
     * 
     * @param request
     * @param contextAttributeStr
     * @param obj
     * @param t
     * @Description:
     */
    @SuppressWarnings("unchecked")
    public static <T> void updateContextList(HttpServletRequest request, String contextAttributeStr, Object obj, T t) {
        
        ServletContext sc = request.getSession().getServletContext();
        List<T> list = (List<T>) sc.getAttribute(contextAttributeStr);
        T objT = (T) obj;
        boolean isExist = false;
        for (T child : list) {
            if (child.equals(objT)) {
                child = objT;
                isExist = true;
                break;
            }
        }
        // 是否存在
        if (!isExist) {
            list.add(objT);
        }
        sc.setAttribute(contextAttributeStr, list);
    }

    public static void updateContextMap(HttpServletRequest request, String contextContactMap, String key, Object obj) {
        
        ServletContext sc = request.getSession().getServletContext();
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) sc.getAttribute(contextContactMap);
        map.put(key, obj);
        sc.setAttribute(contextContactMap, map);
    }

    /**
     * 
     * @param <T>
     * @param request
     * @param contextContactList
     * @param contact
     */
    public static <T> void delete(HttpServletRequest request, String contextPara, Object obj, T t) {
        
        ServletContext sc = request.getSession().getServletContext();
        @SuppressWarnings("unchecked")
        List<T> objList = (List<T>) sc.getAttribute(contextPara);
        for (T child : objList) {
            if(child.equals(obj)){
                obj = (T)child;
                break;
            }
        }
        objList.remove(obj);
        sc.setAttribute(contextPara, objList);
    }

    public static void delete(HttpServletRequest request, String contextParamMap, String key) {
        
        ServletContext sc = request.getSession().getServletContext();
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) sc.getAttribute(contextParamMap);
        map.remove(key);
        sc.setAttribute(contextParamMap, map);
    }
}
